import pygame
import time


class TextRenderer:
    def __init__(self, font_path=None, font_size=36, default_color=(0, 0, 0)):
        pygame.font.init()
        self.font = pygame.font.SysFont(
            font_path, font_size) if font_path else pygame.font.SysFont(None, font_size)
        self.default_color = default_color

    def render_text(self, text, position, antialiased=True):
        # text_surface = self.font.render(text=text, antialias=antialiased, color=self.default_color)
        text_surface = self.font.render(text, antialiased,  self.default_color)
        return text_surface, text_surface.get_rect(topleft=position)

    def draw_text(self, screen, text, position, antialiased=True):
        text_surface, text_rect = self.render_text(
            text=text, position=position, antialiased=antialiased)
        screen.blit(text_surface, text_rect)

# 使用示例


def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 400))
    pygame.display.set_caption("Pygame Text Renderer Class")

    text_renderer = TextRenderer(font_size=36)

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # screen.fill((0, 0, 0))
        screen.fill((255, 255, 255))

        # 在屏幕中央绘制文本
        center_pos = (screen.get_width() // 2, screen.get_height() // 2)
        text_renderer.draw_text(screen, "Hello, Pygame!", center_pos)

        pygame.display.flip()

    pygame.quit()


class Game:
    def __init__(self):
        self.running = True
        self.screen = pygame.display.set_mode((600, 400))
        self.clock = pygame.time.Clock()
        self.text_renderer = TextRenderer(font_size=36)

    def run(self):
        while self.running:
            self.clock.tick(60)
            self.events()    # 处理事件
            self.update()    # 更新游戏状态
            self.draw()      # 绘制屏幕

    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False

    def update(self):
        pass

    def draw(self, x, y):
        self.screen.fill((255, 255, 255))
        center_pos = (self.screen.get_width() // 5 + x,
                      self.screen.get_height() // 5 + y)
        self.text_renderer.draw_text(self.screen, "Hello, Pygame!", center_pos)

        pygame.display.update()  # 更新屏幕

    def __quit__(self):
        pygame.quit()


if __name__ == "__main__":
    # main()

    game = Game()
    # game.run()
    i = 0
    while i < 1000:
        game.draw(i, i)
        time.sleep(1)

        i += 1
        print(i, end='\r')
